<template>
  <div id="userRegisterPage">
    <h1 class="title">月照西山小船来</h1>
    <a-form
      class="register-form"
      :model="formState"
      name="basic"
      label-align="left"
      :label-col="{ span: 5 }"
      :wrapper-col="{ span: 16 }"
      autocomplete="off"
      @finish="onFinish"
      @finishFailed="onFinishFailed"
    >
      <a-form-item
        label="用户名"
        name="username"
        :rules="[{ required: true, message: '请输入用户名' }]"
      >
        <a-input v-model:value="formState.username" />
      </a-form-item>

      <a-form-item
        label="密码"
        name="password"
        :rules="[{ required: true, message: '请输入密码' }]"
      >
        <a-input-password v-model:value="formState.password" />
      </a-form-item>

      <a-form-item
        label="确认密码"
        name="passwordEnsure"
        :rules="[{ required: true, message: '请输入确认密码' }]"
      >
        <a-input-password v-model:value="formState.passwordEnsure" />
      </a-form-item>

      <a-form-item
        label="姓名"
        name="realName"
        :rules="[{ required: true, message: '请输入姓名' }]"
      >
        <a-input v-model:value="formState.realName" />
      </a-form-item>

      <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
        <a-button type="primary" html-type="submit">注册</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { userRegister } from '@/router/api.ts'
import router from '@/router'
import { message } from 'ant-design-vue'

interface FormState {
  username: string;
  password: string;
  passwordEnsure: string;
  realName: string;
}

const formState = reactive<FormState>({
  username: '',
  password: '',
  passwordEnsure: '',
  realName: ''
})

const onFinish = (values: any) => {
  console.log('Success:', values)
  userRegister(values).then(() => {
    message.success('注册成功')
    router.push({
      path: '/user/login',
      replace: true,
    })
  })
}

const onFinishFailed = (errorInfo: any) => {
  console.log('Failed:', errorInfo)
}
</script>
<style scoped>
#userRegisterPage .register-form {
  max-width: 400px;
  margin: 0 auto;
}

#userRegisterPage .title {
  text-align: center;
  margin-bottom: 8px;
}
</style>
